package org.vehicle.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vehicle.entity.VehicleInfo;
import org.vehicle.entity.security.MyUserDetail;
import org.vehicle.service.VehicleInfoService;


@Controller
public class DeviceInfoController {
	private static Logger logger = LogManager.getLogger(DeviceInfoController.class.getName());
	@Autowired
	private VehicleInfoService VehicleInfoService;
	
	@RequestMapping("deviceInfo")
	public String blacklistInfo(HttpServletRequest req, Model model){
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		model.addAttribute("manager", ud);
		return "deviceInfo";
	}
	/**
	 * 获取司机信息分页
	 * @param request
	 * @return
	 */
	@RequestMapping("/getDeviceInfoPage")
    @ResponseBody
    public Map<String, Object> getDeviceInfoPage(HttpServletRequest request) {
    	Map<String, Object> map = new HashMap<String, Object>();
        String currentPage = request.getParameter("curr");
        String pageSize = request.getParameter("pageSize");
        String deviceSN = request.getParameter("deviceSN");
        String vehicleLicense = request.getParameter("vehicleLicense");
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("deviceSN", deviceSN);
            paramMap.put("vehicleLicense", vehicleLicense);
            map = VehicleInfoService.getDeviceInfoPage(paramMap);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return map;
    }
	/**
	 * 添加设备信息
	 * @param req
	 * @return
	 */
	@RequestMapping("/addDeviceInfo")
	@ResponseBody
	public Map<String, Object> addDeviceInfo(VehicleInfo vehicleInfo){
		Map<String, Object> paramMap=new HashMap<String, Object>();
		try{
			Map<String, Object> paramMap2=new HashMap<String, Object>();
			paramMap2.put("deviceSN", vehicleInfo.getDeviceSN());
			VehicleInfo testVehicleInfo=VehicleInfoService.findVehicleInfoByCode(paramMap2);
			if(testVehicleInfo!=null){
				paramMap.put("isSuccess", 0);
				paramMap.put("errMsg", "添加失败，设备标识已经存在");
			}else{
				testVehicleInfo =VehicleInfoService.findVehicleInfoByVehicleLicense(vehicleInfo.getVehicleLicense());
				if(testVehicleInfo==null){
					vehicleInfo.setVehicleType(1);
					vehicleInfo.setState(1);
					vehicleInfo.setCreateDate(new Date());
					VehicleInfoService.insertSelective(vehicleInfo);
				}else{
					testVehicleInfo.setVehicleType(1);
					testVehicleInfo.setDeviceSN(vehicleInfo.getDeviceSN());
					VehicleInfoService.updateByPrimaryKeySelective(testVehicleInfo);
				}
				paramMap.put("isSuccess", 1);
				paramMap.put("errMsg", null);
			}
		}catch(Exception e){
			logger.error("添加设备异常", e);
			paramMap.put("isSuccess", 0);
			paramMap.put("errMsg", "添加失败");
		}
		return paramMap;
	}
	
	/**
	 * 删除设备
	 * @param req
	 * @return
	 */
	@RequestMapping("/delDeviceInfo")
	@ResponseBody
	public Map<String, Object> delDeviceInfo(VehicleInfo vehicleInfo){
		Map<String, Object> paramMap=new HashMap<String, Object>();
		try{
			vehicleInfo=VehicleInfoService.selectByPrimaryKey(vehicleInfo.getVehicleInfoId());
			vehicleInfo.setDeviceSN(null);
			vehicleInfo.setVehicleType(0);
			VehicleInfoService.updateByPrimaryKey(vehicleInfo);
			paramMap.put("isSuccess", 1);
			paramMap.put("errMsg", null);
		}catch(Exception e){
			logger.error("删除设备异常", e);
			paramMap.put("isSuccess", 0);
			paramMap.put("errMsg", "删除失败");
		}
		return paramMap;
	}
}
